﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utilities;

namespace PresentationLayer
{
    public partial class frmDocFileExcel : Form
    {
        public DataTable Result { get; set; }
        public frmDocFileExcel()
        {
            InitializeComponent();
            btnOK.Enabled = false;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = openFileDialog.FileName;
                cbSheetNames.Items.Clear();
                foreach (string s in ExcelReader.GetSheetNameFromExcelFile(txtFileName.Text))
                    cbSheetNames.Items.Add(s);
                if (cbSheetNames.Items.Count > 0)
                {
                    btnOK.Enabled = true;
                    cbSheetNames.SelectedIndex = 0;
                }
                else
                {
                    MessageBox.Show("Tập tin này không có Sheet nào!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    btnOK.Enabled = false;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Result = null;
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Result = ExcelReader.GetDataTableFromExcelFile(txtFileName.Text, cbSheetNames.Text);
            if (Result.Rows.Count == 0)
            {
                Result = null;
                MessageBox.Show("Dữ liệu rỗng!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            Close();
        }
    }
}
